package cn.com.twoke.application.game.mario.game.util;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.InputStream;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class TextureUtils {
    // 图像缓存
    private static final Map<String, BufferedImage> imageCache = new ConcurrentHashMap<>();
    
    // 处理后图像缓存
    private static final Map<String, BufferedImage> processedImageCache = new ConcurrentHashMap<>();

    /**
     * 修改像素点颜色
     * @param original 原始图像
     * @param sourceColor 源颜色数组
     * @param targetColor 目标颜色数组
     * @return 处理后的图像
     */
    public static BufferedImage changeColor(BufferedImage original, int sourceColor[], int targetColor[]) {
        // 创建缓存key
        StringBuilder keyBuilder = new StringBuilder("changeColor_");
        keyBuilder.append(original.hashCode()).append("_");
        for (int color : sourceColor) {
            keyBuilder.append(color).append(",");
        }
        keyBuilder.append("_");
        for (int color : targetColor) {
            keyBuilder.append(color).append(",");
        }
        String key = keyBuilder.toString();
        
        // 检查缓存中是否已存在处理后的图像
        if (processedImageCache.containsKey(key)) {
            return processedImageCache.get(key);
        }
        
        // 处理图像
        BufferedImage copyImage = new BufferedImage(original.getWidth(), original.getHeight(), BufferedImage.TYPE_INT_ARGB);
        for (int x = 0; x < copyImage.getWidth(); x++) {
            for (int y = 0; y < copyImage.getHeight(); y++) {
                int rgb = original.getRGB(x, y);
                int alpha = (rgb >> 24) & 0xff;
                for (int i = 0; i < sourceColor.length; i++) {
                    if ((rgb & 0xFFFFFF) == sourceColor[i] && alpha == 255) {
                        rgb = new Color(targetColor[i]).getRGB();
                        break;
                    }
                }
                copyImage.setRGB(x, y, rgb);
            }
        }
        
        // 将处理后的图像存入缓存
        processedImageCache.put(key, copyImage);
        return copyImage;
    }

    /**
     * 加载图像资源
     * @param name 图像资源名称
     * @return BufferedImage
     */
    public static BufferedImage loadImage(String name) {
        // 检查缓存中是否已存在该图像
        if (imageCache.containsKey(name)) {
            return imageCache.get(name);
        }
        
        // 从资源文件中加载图像
        try (InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(name)) {
            BufferedImage image = ImageIO.read(is);
            // 将加载的图像存入缓存
            imageCache.put(name, image);
            return image;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 水平翻转图像
     * @param image 原始图像
     * @return 翻转后的图像
     */
    public static BufferedImage reverse(BufferedImage image) {
        String key = "reverse_" + image.hashCode();
        
        // 检查缓存中是否已存在翻转后的图像
        if (processedImageCache.containsKey(key)) {
            return processedImageCache.get(key);
        }
        
        // 翻转图像
        int width = image.getWidth();
        int height = image.getHeight();
        BufferedImage newImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
        for (int x = 0; x < width; x++) {
            for (int y = 0; y < height; y++) {
                int rgb = image.getRGB(x, y);
                int alpha = (rgb >> 24) & 0xff;
                int red = (rgb >> 16) & 0xff;
                int green = (rgb >> 8) & 0xff;
                int blue = (rgb) & 0xff;
                newImage.setRGB(width - 1 - x, y, new Color(red, green, blue, alpha).getRGB());
            }
        }
        
        // 将翻转后的图像存入缓存
        processedImageCache.put(key, newImage);
        return newImage;
    }
    
    /**
     * 清除所有缓存
     */
    public static void clearCache() {
        imageCache.clear();
        processedImageCache.clear();
    }
    
    /**
     * 获取缓存统计信息
     * @return 缓存统计信息
     */
    public static String getCacheStats() {
        return String.format("Image Cache: %d, Processed Image Cache: %d", 
            imageCache.size(), processedImageCache.size());
    }
}